package com.health.userbackstage.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.health.userbackstage.mapper.CheckGroupMapper;
import com.health.userbackstage.mapper.CheckItemMapper;
import com.health.userbackstage.mapper.relationmapper.CheckGroupCheckItemMapper;
import com.health.userbackstage.pojo.CheckGroup;
import com.health.userbackstage.pojo.CheckItem;
import com.health.userbackstage.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Autowired
    private CheckGroupCheckItemMapper checkGroupCheckItemMapper;

    public List<CheckGroup> findAll() {
        return checkGroupMapper.findAll();
    }

    public int update(CheckGroup checkGroup) {
        return checkGroupMapper.updateById(checkGroup);
    }

    @Transactional(rollbackFor = Exception.class)
    public int add(CheckGroup checkGroup) {
        int cg = checkGroupMapper.insert(checkGroup);


        List<Integer> ciids = checkGroup.getCiid();
        for (Integer ciid : ciids) {
            checkGroupCheckItemMapper.add(checkGroup.getId(), ciid);
        }

        return cg;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean del(Integer id) {
        checkGroupMapper.deleteById(id);

        checkGroupCheckItemMapper.del(id);

        return true;
    }


    public List<Integer> findciid(Integer cgid) {
        return checkGroupMapper.findCiid(cgid);
    }

    @Override
    public CheckGroup selectOneByCode(CheckGroup checkGroup) {
        return checkGroupMapper.selectOneByCode(checkGroup);
    }

    @Override
    public CheckGroup selectOneByName(CheckGroup checkGroup) {
        return checkGroupMapper.selectOneByName(checkGroup);
    }

    @Override
    public CheckGroup selectOneByHelpCode(CheckGroup checkGroup) {
        return checkGroupMapper.selectOneByHelpCode(checkGroup);
    }

    //分页查询
    public PageInfo<CheckGroup> findPage(int pageNum, int pageSize) {

        PageHelper.startPage(pageNum, pageSize);

        List<CheckGroup> id = checkGroupMapper.findAll();

        return new PageInfo<>(id);
    }
}
